﻿Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Service1
    Inherits System.Web.Services.WebService

    Shared listofusers As New List(Of Users)

    <WebMethod()> _
    Public Function HelloWorld() As String
       Return "Hello World"
    End Function

    <WebMethod>
    Public Sub AddUser(ByVal Appid As String, ByVal Userid As String, ByVal Channel As String)
        '        listofusers.Add(New Users With {.Appid = Appid, .UserId = Userid, .Channel = Channel})
        'Using w As System.IO.StreamWriter = New System.IO.StreamWriter(Server.MapPath("~/user.txt"), True)
        '    Dim aa = (From b In listofusers
        '                        Where b.Appid = Appid And b.UserId = Userid
        '                        Select b).FirstOrDefault

        '    If aa Is Nothing Then
        '        listofusers.Add(New Users With {.Appid = Appid, .UserId = Userid, .Channel = Channel})
        '        w.WriteLine(Appid & "|" & Userid & "|" & Channel & "#")
        '    End If
        'End Using
        Me.AddUserToXmlFile(Appid, Userid, Channel)
    End Sub

    <WebMethod>
    Public Sub RemoveUser(ByVal Appid As String, ByVal UserId As String)
        Dim f = (From a In listofusers
                Where a.UserId = UserId And a.Appid = Appid
                Select a).FirstOrDefault

        If f IsNot Nothing Then
            listofusers.Remove(f)
        End If
    End Sub

    <WebMethod>
    Public Function GetUsers() As List(Of Users)
        Return listofusers
    End Function

    
    Public Sub New()

        listofusers = ReadAllUsersFromXMLFile()
        'If System.IO.File.Exists(Server.MapPath("~/user.txt")) Then
        '    Using w As System.IO.StreamReader = New System.IO.StreamReader(Server.MapPath("~/user.txt"))
        '        Dim line = w.ReadToEnd

        '        Dim lines = Split(line, "#")
        '        For Each a In lines
        '            Debug.WriteLine(a)
        '            If a <> "" Then
        '                Dim ass() = Split(a.Trim.Replace(" ", "").Trim, "|")
        '                Dim aa As New Users

        '                aa.Appid = ass(0)
        '                aa.UserId = ass(1)
        '                aa.Channel = ass(2).Replace("#", "").TrimEnd

        '                listofusers.Add(aa)
        '            End If

        '        Next
        '    End Using
        'End If

        'Using w As System.IO.StreamWriter = New System.IO.StreamWriter(Server.MapPath("~/user.txt"), True)
        '    For Each a In listofusers
        '        w.WriteLine(a.Appid & "|" & a.UserId & "|" & a.Channel & "#")
        '    Next
        'End Using

    End Sub

    Protected Overrides Sub Finalize()

        'Using w As System.IO.StreamWriter = New System.IO.StreamWriter(Server.MapPath("~/user.txt"), True)
        '    For Each a In listofusers
        '        w.WriteLine(a.Appid & "|" & a.UserId & "|" & a.Channel & "#")

        '    Next
        'End Using
       
        MyBase.Finalize()
    End Sub

    Private Function MyFile() As String
        Return Server.MapPath("~/users.xml")
    End Function

    Function CreateXML() As XDocument
        If System.IO.File.Exists(MyFile) Then
            Return XDocument.Load(MyFile)
        Else
            Dim xdoc As New XDocument
            Dim Root As XElement = <Users>

                                   </Users>
            xdoc.Add(Root)
            xdoc.Save(MyFile)
            Return xdoc
        End If
    End Function

    Private Sub AddUserToXmlFile(ByVal Appid As String, ByVal UserId As String, ByVal Channel As String)
        Dim xdoc = CreateXML()
        Dim newElement As XElement = <User>
                                         <appid><%= Appid %></appid>
                                         <userid><%= UserId %></userid>
                                         <channel><%= Channel %></channel>
                                     </User>
        xdoc.Root.Add(newElement)
        xdoc.Save(MyFile)
    End Sub

    Private Function ReadAllUsersFromXMLFile() As List(Of Users)
        Dim retlist As New List(Of Users)
        Dim xdoc = CreateXML()
        For Each a In xdoc.Root.Elements
            Dim nuser As New Users
            With nuser
                .Appid = a.<appid>.Value.ToString
                .UserId = a.<userid>.Value.ToString
                .Channel = a.<channel>.Value.ToString
            End With
            retlist.Add(nuser)
        Next
        Return retlist
    End Function
End Class

Public Class Users
    Property Appid As String
    Property UserId As String
    Property Channel As String
End Class